package com.china08.yxyapi.api.home.kecheng.cloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

/**
 * 
 * @annotation: 云课程--推荐课程查询
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudLessonRecommedSrhController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;
    @Autowired
    private YxyLessonsCloudCatalogRepository yxyLessonsCloudCatalogRepository;

    @Autowired
    private YxyLessonsCloudLessonItemRepository yxyLessonsCloudLessonItemRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/recommmed/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Integer level = reqModel.level;
        String lessonId = reqModel.lessonId;
        List<YxyLessonsCloudLesson> lessons = null;
        List<String> catalogIdList = new ArrayList<String>();
        if (level == 1) {
            // 获取本校所有一级目录ID
            List<YxyLessonsCloudCatalog> catalogs = yxyLessonsCloudCatalogRepository.findByLevel(1);
            for (YxyLessonsCloudCatalog catalog : catalogs) {
                catalogIdList.add(catalog.getId());
            }
            // 根据所有一级目录id 查出前两个学习人数最多的一级下(包括二三四五级)的课程
            lessons = yxyLessonsCloudLessonRepository
                    .findTop2ByStatusAndIdNotAndIsVisibleAndCatalogId1InOrderByVvDesc(2, lessonId, 1, catalogIdList);
        } else if (level == 2) {
            // 获取本校所有二级目录ID
            List<YxyLessonsCloudCatalog> catalogs = yxyLessonsCloudCatalogRepository.findByLevel(2);
            for (YxyLessonsCloudCatalog catalog : catalogs) {
                catalogIdList.add(catalog.getId());
            }
            // 根据所有二级目录id 查出前两个学习人数最多的二级下(包括三四五级)的课程
            lessons = yxyLessonsCloudLessonRepository
                    .findTop2ByStatusAndIdNotAndIsVisibleAndCatalogId2InOrderByVvDesc(2, lessonId, 1, catalogIdList);
        } else if (level == 3) {
            // 获取本校所有三级目录ID
            List<YxyLessonsCloudCatalog> catalogs = yxyLessonsCloudCatalogRepository.findByLevel(3);
            for (YxyLessonsCloudCatalog catalog : catalogs) {
                catalogIdList.add(catalog.getId());
            }
            // 根据所有三级目录id 查出前两个学习人数最多的三级下(包括三四五级)的课程
            lessons = yxyLessonsCloudLessonRepository
                    .findTop2ByStatusAndIdNotAndIsVisibleAndCatalogId3InOrderByVvDesc(2, lessonId, 1, catalogIdList);
        } else if (level == 4) {
            // 获取本校所有四级目录ID
            List<YxyLessonsCloudCatalog> catalogs = yxyLessonsCloudCatalogRepository.findByLevel(4);
            for (YxyLessonsCloudCatalog catalog : catalogs) {
                catalogIdList.add(catalog.getId());
            }
            // 根据所有四级目录id 查出前两个学习人数最多的一级下(包括四五级)的课程
            lessons = yxyLessonsCloudLessonRepository
                    .findTop2ByStatusAndIdNotAndIsVisibleAndCatalogId4InOrderByVvDesc(2, lessonId, 1, catalogIdList);
        } else if (level == 5) {
            // 获取本校所有五级目录ID
            List<YxyLessonsCloudCatalog> catalogs = yxyLessonsCloudCatalogRepository.findByLevel(5);
            for (YxyLessonsCloudCatalog catalog : catalogs) {
                catalogIdList.add(catalog.getId());
            }
            // 根据所有五级目录id 查出前两个学习人数最多的五级下的课程
            lessons = yxyLessonsCloudLessonRepository.findTop2ByStatusAndIsVisibleAndCatalogId5InOrderByVvDesc(2, 1,
                    catalogIdList);
        }
        List<RespModel> respModels = new ArrayList<KechengCloudLessonRecommedSrhController.RespModel>();
        if (lessons != null && lessons.size() > 0) {
            for (YxyLessonsCloudLesson lesson : lessons) {
                RespModel model = new RespModel();
                YxyLessonsCloudCatalog yxyLessonsCloudCatalog = null;
                // 获取最末节的目录名称
                if (StringUtils.isNotBlank(lesson.getCatalogId5())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository.findById(lesson.getCatalogId5());
                } else if (StringUtils.isNotBlank(lesson.getCatalogId4())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository.findById(lesson.getCatalogId4());
                } else if (StringUtils.isNotBlank(lesson.getCatalogId3())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository.findById(lesson.getCatalogId3());
                } else if (StringUtils.isNotBlank(lesson.getCatalogId2())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository.findById(lesson.getCatalogId2());
                } else if (StringUtils.isNotBlank(lesson.getCatalogId1())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository.findById(lesson.getCatalogId1());
                }
                model.catalogName = yxyLessonsCloudCatalog.getName();
                model.count = yxyLessonsCloudLessonItemRepository.countByLessonIdAndStatus(lesson.getId(), 2);
                model.cover = lesson.getCover();
                model.ep = lesson.getEp();
                model.EpTimes = lesson.getEpTimes();
                model.lessonId = lesson.getId();
                model.name = lesson.getName();
                model.vv = lesson.getVv();
                model.author = lesson.getAuthor();
                model.schoolName = lesson.getSchoolNick();
                model.isJing = lesson.getIsJingpinLesson();
                model.clickPv = lesson.getClickPv() == null ? 0 : lesson.getClickPv();
                respModels.add(model);
            }
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 目录等级
        public Integer level;
        // 课程ID
        public String lessonId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 课程名称
        public String name;
        // 课程评分
        public Float ep;
        // 评价人数
        public Integer EpTimes;
        // 学习人数
        public Integer vv;
        // 点击次数
        public Integer clickPv;
        // 课程Id
        public String lessonId;
        // 所属目录（最后一级目录名称）
        public String catalogName;
        // 贡献者
        public String author;
        // 贡献者学校
        public String schoolName;
        // 是否精品
        public Integer isJing;
    }
}
